package com.navidove.web.manager.ctrl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.navidove.appspotlight.model.bo.SysUser;
import com.navidove.appspotlight.model.service.SysUserService;
import com.navidove.commons.exception.DataDuplicatedException;
import com.navidove.commons.utils.DateUtils;
import com.navidove.commons.utils.I18NUtil;


@Controller
@RequestMapping("/system/sysUser")
public class SysUserEditCtrl {

	protected final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
	
    @Autowired
    private SysUserService sysUserService;
    
    
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DateUtils.DEFAULT_DATE_PATTERN);
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
        binder.setDisallowedFields("isSupervisor");
    }
    
    @ModelAttribute
    public void reset(Model model, @RequestParam(value="id", required=false) Long id) {        

        SysUser sysUser = null;
        if (id == null) {
            sysUser = new SysUser();
            sysUser.setEnable(Boolean.TRUE);
        } else {
            sysUser = sysUserService.getSysUser(id);
        }
        model.addAttribute("sysUser", sysUser);       
    }
    
    
    @RequestMapping("/add")
    public String add(Model model) {
        return "/system/sysUser_edit";
    }
    
    @RequestMapping("/edit")
    public String edit(Model model) {
        return "/system/sysUser_edit";
    }
    
    @ResponseBody
    @RequestMapping("/save")
    public Map<String, Object> save(Model model, @ModelAttribute("sysUser") SysUser sysUser) {

        String success = I18NUtil.getMsg("dialog.message.data.param.success", I18NUtil.getMsg("button.save"));
        String failure = I18NUtil.getMsg("dialog.message.data.param.failure", I18NUtil.getMsg("button.save"));
        
        Map<String, Object> rtnVal = new HashMap<String, Object>();        
 
        try {
            sysUserService.saveSysUser(sysUser);
            rtnVal.put("message", success);
            rtnVal.put("success", Boolean.TRUE);
            rtnVal.put("data", sysUser.getId());
        } catch (DataDuplicatedException e) {
            logger.error(e.getMessage(),e);
            rtnVal.put("message", I18NUtil.getMsg("dialog.message.param.not.duplicated",
                    I18NUtil.getMsg("system.bo.SysUser.login")));
            rtnVal.put("success", Boolean.FALSE);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            rtnVal.put("message", failure);
            rtnVal.put("success", Boolean.FALSE);
        }

        return rtnVal;
    }    
}
